// StorageCom.cpp: implementation of the CStorageCom class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "StorageCom.h"

#include <atlbase.h>
#include <memory>
#include "ItemsMap.h"
#include "eDASexpressApp.h"

extern CEDASexpressApp theApp;

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CStorageCom::CStorageCom()
	: m_pOleException(new COleException)
{
	if (!m_IAuthority.CreateDispatch( "Activcard.Authority.1", m_pOleException ))
		throw m_pOleException;
}

CStorageCom::~CStorageCom()
{
	if (m_pOleException)
		m_pOleException->Delete();
}

void CStorageCom::GetRoles(CCopyableMap<CString,LPCTSTR,CStringItemsArray,CStringItemsArray&> & mapRoles)
{
	CComBSTR cbstrRoles;
		int iRet= m_IAuthority.GetRoles( &cbstrRoles );
	if (S_OK == iRet)
	{
		CString strRoles(cbstrRoles);
		CItemsMap<CString,LPCTSTR,CStringItemsArray,CStringItemsArray&> itemsmapRoles(strRoles);
		mapRoles = itemsmapRoles;
	}
	return;
}

void CStorageCom::AddRole(const CString &strRoleName, const CStringItemsArray &arrAdmittanceRadiusIPs)
{
	m_IAuthority.AddRole( strRoleName, arrAdmittanceRadiusIPs.GetSerialData() );
}

void CStorageCom::UpdateRole(const CString &strOriginalRoleName, const CString &strNewRoleName, const CStringItemsArray &arrAdmittanceRadiusIPs)
{
	m_IAuthority.UpdateRole( strOriginalRoleName, strNewRoleName, arrAdmittanceRadiusIPs.GetSerialData() );
}

void CStorageCom::DeleteRole(const CString &strRoleName)
{
	m_IAuthority.DeleteRole( strRoleName );
}

void CStorageCom::SetUserGroupDefaultRole(const CString &strGroupName, const CStringItemsArray &arrDefaultRoles)
{
	m_IAuthority.SetUserGroupDefaultRole( strGroupName, arrDefaultRoles.GetSerialData() );
}

void CStorageCom::SetUserRole(const CString &strUserName, const CStringItemsArray &arrRolesNames)
{
	m_IAuthority.SetUserRole( strUserName, arrRolesNames.GetSerialData() );

	if (NULL != theApp.m_pMainDlg->m_KUser.m_puserpages)
		theApp.m_pMainDlg->m_KUser.m_puserpages->SetRefresh();
}